package com.example.zhc36.fragment.frag;

import android.content.Intent;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioGroup;


import com.example.zhc36.fragment.R;
import com.example.zhc36.fragment.utils.app.BaseFragment;

/**
 *
 */
public class MainMainFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener {

    private FrameLayout fl;
    private RadioGroup rg;
    private Fragment[] fregs;
    private Fragment[] frags_add = new Fragment[3];

    @Override
    protected View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {

        View view = inflater.inflate(R.layout.activity_main, container, false);

        fl = view.findViewById(R.id.fl);
        rg = view.findViewById(R.id.rg);

        return view;
    }

    @Override
    public void initData() {
        fregs = new Fragment[]{new MainMsgFragment(),new MainlxrFragment(), new MaindtFragment(),};


    }

    @Override
    protected void initListener() {
        rg.setOnCheckedChangeListener(this);
    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.massage: {
                changeFragment(0);

            }
            break;
            case R.id.lxr: {
                changeFragment(1);


            }
            break;
            case R.id.dt: {

                changeFragment(2);

            }
            break;


        }


    }
    public void changeFragment(int index){
        FragmentTransaction tran = getChildFragmentManager().beginTransaction();
        for (Fragment frm : frags_add) {
            if(frm!=null){
                tran.hide(frm);
            }}
        if (frags_add[index] == null) {
            frags_add[index] = fregs[index];
            tran.add(R.id.fl, frags_add[index]);

        } else {

            tran.show(frags_add[index]);

        }


        tran.commit();



    }


}
